function visit_deepseek_chat() {
  // chrome.tabs.create({ url: "https://chat.deepseek.com/" })
  chrome.tabs.query({ url: 'https://chat.deepseek.com/*' }, (result) => {
    chrome.storage.local.get({ reuseChatPage: true }, (items) => {
      if (result.length > 0 && items.reuseChatPage) {
        chrome.tabs.update(result[0].id, { active: true })
      } else {
        chrome.tabs.create({ url: "https://chat.deepseek.com/" })
      }
    })
  })
}

chrome.action.onClicked.addListener(() => {
  visit_deepseek_chat()
})

chrome.runtime.onInstalled.addListener(() => {
  chrome.contextMenus.removeAll()
  chrome.contextMenus.create({
    id: 'start-chat',
    title: '前往 DeepSeek 对话'
  })
})

chrome.contextMenus.onClicked.addListener((info) => {
  if (info.menuItemId == 'start-chat') {
    visit_deepseek_chat()
  }
})

chrome.runtime.onMessage.addListener((message) => {
  chrome.downloads.download({
    url: message.objectUrl,
    filename: `${message.filenamePrefix}${Date.now()}.txt`
  })
})